package terra.awt;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.text.DecimalFormat;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import terra.managers.MapManager;
import terra.model.map.Map;
import terra.network.Connections;
import terra.network.Server;

public class Frame extends JFrame implements Runnable {

	private static final long serialVersionUID = 5357852692294432221L;
	JLabel conn, maps, mem;
	DecimalFormat format = new DecimalFormat();

	public Frame() {
		format.applyPattern("#0.0");
		setTitle("Server");
		setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setMinimumSize(new Dimension(450, 300));

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JProgressBar progressBar = new JProgressBar();
		progressBar.setBounds(10, 246, 150, 16);
		panel.add(progressBar);

		JLabel lblProgress = new JLabel("progress");
		lblProgress.setBounds(10, 221, 46, 14);
		panel.add(lblProgress);

		JList list = new JList();
		list.setBounds(304, 47, 128, 159);
		panel.add(list);

		JLabel lblConnections = new JLabel("Connections");
		lblConnections.setBounds(304, 11, 128, 14);
		panel.add(lblConnections);

		JLabel lblStatistics = new JLabel("Statistics");
		lblStatistics.setBounds(10, 11, 80, 14);
		panel.add(lblStatistics);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 36, 284, 2);
		panel.add(separator);

		JLabel lblUsedMem = new JLabel("memory used/free");
		lblUsedMem.setBounds(10, 48, 180, 14);
		panel.add(lblUsedMem);

		mem = new JLabel("0 Mb");
		mem.setHorizontalAlignment(SwingConstants.RIGHT);
		mem.setBounds(214, 48, 80, 14);
		panel.add(mem);

		JLabel lblConnections_1 = new JLabel("Connections");
		lblConnections_1.setBounds(10, 98, 128, 14);
		panel.add(lblConnections_1);

		maps = new JLabel("0/0");
		maps.setHorizontalAlignment(SwingConstants.RIGHT);
		maps.setBounds(214, 73, 80, 14);
		panel.add(maps);

		JLabel lblTotalMaps = new JLabel("maps total/active");
		lblTotalMaps.setBounds(10, 73, 180, 14);
		panel.add(lblTotalMaps);

		conn = new JLabel("0");
		conn.setHorizontalAlignment(SwingConstants.RIGHT);
		conn.setBounds(214, 98, 80, 14);
		panel.add(conn);

		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(304, 36, 131, 2);
		panel.add(separator_1);

	}

	@Override
	public void run() {
		double freeMb = (Runtime.getRuntime().freeMemory() / 1024d / 1024d);
		double used = (Runtime.getRuntime().totalMemory() / 1024d / 1024d);
		used -= freeMb;
		mem.setText(format.format(used) + " / " + format.format(freeMb) + " M");
		conn.setText(Server.getInstance().all.size() + " "
				+ Connections.connections.size());
		if (MapManager.getInstance().init) {
			int active = 0;
			for (Map m : MapManager.getInstance().getMaps().values()) {
				if (m.active)
					active++;
			}
			maps.setText(MapManager.getInstance().getMaps().size() + " / "
					+ active);
		}
	}

}
